<?php


require_once 'Zend/Db/Table/Abstract.php';
class Model_AccountMapper extends Zend_Db_Table_Abstract {
	
	protected $_name = 'account';
	protected $_primary = 'AccID';
	protected $_sequence = true;
	
	public function createNewAccount(Model_Account $acc, $address){
		
		try {
			$data=array(
					'FirstName'     => $acc->getFirstName(),
					'LastName' 		=> $acc->getLastName(),
					'Username'      => $acc->getUsername(),
					'Email'			=> $acc->getEmail(),
					'Password'		=> $acc->getPassword(),
					'Gender'		=> $acc->getGender(),
					'DateOfBirth'	=> $acc->getDateOfBirth(),
					'Phone'			=> $acc->getPhone(),
			);
			$this->insert($data);
			$user=$this->accountDetail($acc->getUsername());
			$data=array(
					'AccID'     	=> $user->getId(),
					'Adress' 			=> $address,
				
			);
			
			$registry 	= Zend_Registry::getInstance();
			$db = $registry['my_db'];
			$db->insert('Customer', $data);
			return true;
		} catch (Exception $e) {
			return false;
		}
		
		
	}
	
	public function isValidUser(Model_Account $acc){
		$username=$acc->getUsername();
		$password=$acc->getPassword();
		$email=$acc->getEmail();
		
		$select  = $this->select()->where('Username = ?', $username)->where('Password = ?', $password);
		$row=$this->fetchRow($select);
		
		if ($row==true)
			return true;
		else return false;
		//Check by Email and Password
	}
	
	public function accountDetail($username)
	{
		
		$select  = $this->select()->where('Username = ?', $username);
		$row=$this->fetchRow($select);
		if ($row){
			$acc=new Model_Account();
			$acc->setId($row['AccID']);
			$acc->setUsername($row['Username']);
			$acc->setPassword($row['Password']);
			$acc->setEmail($row['Email']);
			$acc->setFirstName($row['FirstName']);
			$acc->setLastName($row['LastName']);
			$acc->setGender($row['Gender']);
			$acc->setDateOfBirth($row['DateOfBirth']);
			$acc->setPhone($row['Phone']);
			$acc->setImage('');

			// getRole;
			
			
			return $acc;
		}
		else {
			return 'Not valid user';
		}
		
	}
	
	public function getRole($username){
		
	}
	
	public function updateAccount(Model_Account $acc)
	{
	
		try {
			$data=array(
					'FirstName'     => $acc->getFirstName(),
					'LastName' 		=> $acc->getLastName(),
					'Username'      => $acc->getUsername(),
					'Email'			=> $acc->getEmail(),
					'Password'		=> $acc->getPassword(),
					'Gender'		=> $acc->getGender(),
					'DateOfBirth'	=> $acc->getDateOfBirth(),
					'Phone'			=> $acc->getPhone(),
					
			);
			$where=Array('Username = ?' => $acc->getUsername()
					);
			$this->update($data, $where);
			return true;
		} catch (Exception $e) {
			return false;
		}
		
	
	}
	
	public function getCurrentAccount(){
		if(Zend_Auth::getInstance()->hasIdentity())
		{
			return Zend_Auth::getInstance()->getIdentity();
		}
		else
		{
			return "";
		}
	}
	
	public function getCustomerId($username){
		$user=$this->accountDetail($username);
		$cusM=new Model_CustomerMapper();
		$cus=$cusM->getCustomer($user->getId());
		if ($cus->getId()==-1){
			return -1;
		}
		else{
			return $cus->getId();
		}
	}
	
}
